package samples.d3.image 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Vector3D;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	import samples.BaseSample;
	import systems.particles.Renderer3D;
	import systems.particles.Viewport;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class ColorSpace extends BaseSample
	{
		
		//[Embed(source = '../../../../lib/penguins.jpg')]private var imageSrc0:Class;
		[Embed(source = '../../../../lib/wheel_HSL.png')]private var imageSrc1:Class;
		[Embed(source = '../../../../lib/lemur1.jpg')]private var imageSrc2:Class;
		[Embed(source = '../../../../lib/fruit0.jpg')]private var imageSrc3:Class;
		[Embed(source = '../../../../lib/fruit1.jpg')]private var imageSrc4:Class;
		private var images:Vector.<Bitmap> = Vector.<Bitmap>([
																//new imageSrc0(),
																new imageSrc1(),
																new imageSrc2(),
																new imageSrc3(),
																new imageSrc4()
																])
		private var image:Bitmap;
		private var bd:BitmapData;
		private var vertices:Vector.<Number>;
		private var vectors:Vector.<Vector3D>;
		private var view:Viewport;
		private var side:int = 0;
		private var currentPic:int = 0;
		private var w:int;
		private var h:int;
		private var scale:Number;
		private var bmp:Bitmap;
		private var available:Vector.<Number>;
		
		public function ColorSpace() 
		{
			
			_stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			_stage.addEventListener( MouseEvent.MOUSE_WHEEL, onWheelHandler );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemove );
			
			
			view = new Viewport( sw, sh, 0 );
			addChild( view );
			
			
			bmp =  new Bitmap( ); 
			addChild(bmp);
			reset();
			
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			currentPic++;
			reset();
		}
		
		private function onRemove(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			_stage.removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			_stage.removeEventListener( MouseEvent.MOUSE_WHEEL, onWheelHandler );
		}
		
		private function reset():void 
		{
			currentPic %= images.length;
			image = images[ currentPic ];
			
			scale = .5;
			if ( currentPic == 0 )scale = 1.5;
			
			w = image.width;
			h = image.height;
			
			bd = new BitmapData( w * scale, h * scale, true, 0 );
			bd.draw( image, new Matrix( scale, 0, 0, scale ) );
			
			//bd.perlinNoise( 50, 50, 3, 0, true, true, 7 );
			bmp.bitmapData = bd;
			bmp.y = 100;
			bmp.width = bmp.height = 100;
			
			w *= scale;
			h *= scale;
			
			available = Vector.<Number>([]);
			available.length = 0xFFFFFF+1;
			
			vertices = Vector.<Number>([]);
			
			vectors = Vector.<Vector3D>([]);
			
			var i:int = w * h, x:Number, y:Number, z:Number;
			while ( i-- )
			{
				
				var color:uint = bd.getPixel( i % w, int( i / w ) );
				if ( color != 0 )
				{
					x = ( color >> 16 & 0xFF ) - 0x80;
					y = ( color >> 8 & 0xFF ) - 0x80;
					z = ( color & 0xFF ) - 0x80;
					if ( available[ color ] == 0 )
					{
						available[ color ] = 1;
						vertices.push( x, y, z );
						vectors.push( new Vector3D( x, y, z ) );
					}
				}
				
			}
			clearInterval( interval );
			interval = setInterval( render, 30 );
			
			
		}
		
		private function onWheelHandler(e:MouseEvent):void 
		{
			
			Renderer3D.cameraPosition.z -= e.delta;
			
		}
		
		private function render():void 
		{
			
			var limX:int = 60;
			Renderer3D.objectRotation.x += ( ( sh/2-mouseY ) - Renderer3D.objectRotation.x ) * .01;
			if ( Renderer3D.objectRotation.x >= limX ) Renderer3D.objectRotation.x = limX;
			if ( Renderer3D.objectRotation.x <= -limX ) Renderer3D.objectRotation.x = -limX;
			
			Renderer3D.objectRotation.y += ( mouseX - Renderer3D.objectRotation.y ) * .1;
			
			view.render( vertices, vectors );
			
		}
		
		
	}

}